import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { MateDataService } from 'src/app/services/mate_data.service';
import { FormLayout } from 'ng-devui/form';
import { SessionService } from 'src/app/services/session.service';

@Component({
    selector: 'app-user-info',
    templateUrl: './user-info.component.html',
    styleUrls: ['./user-info.component.scss']
})
export class UserInfoComponent implements OnInit {

    layoutDirection: FormLayout = FormLayout.Vertical;
    getUserParam: any;
    savedCurrentUserInfo: any;
    currentUserInfo: any = {};
    msgs: Array<Object> = [];
    options = ['normal', 'admin'];
    canEditRole: boolean = true;
    constructor(
        private userRouter: ActivatedRoute,
        private router: Router,
        private mateDataSrv: MateDataService,
        private sessionSrv: SessionService
    ) { }

    async ngOnInit() {
        this.userRouter.queryParams.subscribe((res: any) => {
            this.getUserParam = {
                userId: res.userId,
            }
            let editUser = this.sessionSrv.getSessionValue('user');
            if (editUser.roleName === "admin" || editUser.roleName === "superAdmin") {
                this.canEditRole = false;
            }
            this.getUserInfo(this.getUserParam);
        })
    }

    async getUserInfo(param) {
        const { status, result } = await this.mateDataSrv.getUserInfo(param) as any;
        if (status.toUpperCase() === 'SUCCESS') {
            console.log('result', result)
            this.currentUserInfo = result;
            this.savedCurrentUserInfo = JSON.parse(JSON.stringify(this.currentUserInfo));
        } else if (status.toUpperCase() === 'TOKEN_FAILURE') {
            this.msgs = [
                { severity: 'error', summary: '错误', content: '登录信息失效！请重新登录！' },
            ];
            this.router.navigate(['login'])
        }
    }


    saveUserInfo() {
        console.log(this.currentUserInfo);
        const editUser = this.sessionSrv.getSessionValue('user');
        const updateUserInfo = { ...this.currentUserInfo, lastModifiedBy: editUser.userName }
        // const updateUserInfo = this.currentUserInfo;
        console.log(updateUserInfo)
        this.mateDataSrv.updateUserInfo(updateUserInfo).subscribe((res: any) => {
            console.log('update-back', res)
            const { status, result } = res;
            if (status.toUpperCase() === 'SUCCESS') {
                this.currentUserInfo = {
                    userId: result.userId,
                    userName: result.userId,
                    userAcc: result.userId,
                    Password: result.userId,
                    role: result.userId
                }
                this.savedCurrentUserInfo = JSON.parse(JSON.stringify(this.currentUserInfo));
            } else if (status.toUpperCase() === 'TOKEN_FAILURE') {
                this.msgs = [
                    { severity: 'error', summary: '错误', content: '登录信息失效！请重新登录！' },
                ];
                this.router.navigate(['login'])
            } else {
                this.msgs = [
                    { severity: 'error', summary: '错误', content: '保存信息失败！' },
                ];
                this.currentUserInfo = this.savedCurrentUserInfo;
            }
        })
    }

    resetUserInfo() {
        this.currentUserInfo = this.savedCurrentUserInfo;
    }


}
